package com.wulis.provider.authority.web;

import static com.wulis.common.model.enums.VersionEnum.WEB_V_1_0;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.wulis.common.annotation.ApiVersion;
import com.wulis.common.annotation.CrossAuth;
import com.wulis.common.support.BaseController;
import com.wulis.common.utils.AuthUserUtil;
import com.wulis.common.wrapper.ApiWrapMapper;
import com.wulis.common.wrapper.ApiWrapper;
import com.wulis.config.security.AuthUserDetails;
import com.wulis.provider.basic.model.vo.QueryUserVo;
import com.wulis.provider.basic.service.UserInfoService;
import com.wulis.provider.authority.model.dto.CodeLoginDto;
import com.wulis.provider.authority.model.dto.FormLoginDto;
import com.wulis.provider.authority.model.dto.SendCodeDto;
import com.wulis.provider.authority.model.vo.QueryMenuTreeVo;
import com.wulis.provider.authority.service.AccountService;
import com.wulis.provider.authority.service.MenuService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author WuliBao
 * @date 2020-04-17
 */
@Slf4j
@ApiSort(-1)
@RestController
@RequestMapping("web")
@Api(tags = "登录")
@AllArgsConstructor
public class LoginController extends BaseController {
    
    private final MenuService menuService;
    
    private final AccountService accountService;
    
    private final UserInfoService userInfoService;
    
    @CrossAuth
    @ApiVersion(WEB_V_1_0)
    @GetMapping("form/login")
    @ApiOperation("表单登录")
    public ApiWrapper<Object> formLogin(@Valid FormLoginDto formLoginDto) {
        return accountService.formLogin(formLoginDto);
    }
    
    @CrossAuth
    @ApiVersion(WEB_V_1_0)
    @GetMapping("code/login")
    @ApiOperation("验证码登录")
    public ApiWrapper<Object> codeLogin(@Valid CodeLoginDto codeLoginDto) {
        return accountService.codeLogin(codeLoginDto);
    }
    
    @CrossAuth
    @ApiVersion(WEB_V_1_0)
    @GetMapping("send/code")
    @ApiOperation("发送证码")
    public ApiWrapper<Object> sendCode(@Valid SendCodeDto sendCodeDto) {
        return accountService.sendCode(sendCodeDto);
    }
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("logout")
    @ApiOperation("注销登录")
    public ApiWrapper<Object> logout() {
        return accountService.logout();
    }
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryUser")
    @ApiOperation("查询用户信息(登录用户信息)")
    public ApiWrapper<QueryUserVo> queryUser() {
        AuthUserDetails authUserDetails = AuthUserUtil.getDetails();
        return ApiWrapMapper.success(userInfoService.queryUser(authUserDetails.getUserInfo().getUserId()));
    }
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryMenuList")
    @ApiOperation("查询菜单列表(登录用户菜单权限列表)")
    public ApiWrapper<List<QueryMenuTreeVo>> queryMenuList() {
        return ApiWrapMapper.success(menuService.queryMenuList());
    }
}
